<template>
  <el-form ref="form" :model="form" :rules="rules" label-width="100px">
    <el-form-item label="手机号码" prop="telephone">
      <el-input placeholder="请输入手机号" v-model="form.telephone" :disabled="true"/>
    </el-form-item>
    <el-form-item label="登录密码" prop="password">
      <el-input placeholder="请输入密码" type="password" v-model="form.password" />
    </el-form-item>
    <el-form-item label="确认密码" prop="password2">
      <el-input placeholder="请输入密码" type="password" v-model="form.password2" />
    </el-form-item>
    <el-form-item label="短信验证码:" prop="authCode">
      <div class="code-box">
        <el-input placeholder="请输入短信验证码" v-model="form.authCode" />
        <CCCountDown @click="onCodeClick" ref="codebtn" />
      </div>
    </el-form-item>
    <el-form-item>
      <el-button class="login-btn" type="primary" @click="onSure">确认修改密码</el-button>
    </el-form-item>
  </el-form>
</template>
<script >
  import CCCountDown from '@/components/common/CCCountDown.vue';
  import { doSmsCode, doUpdatePwd, doLogout } from '@/apis/user';
  import ZZToast from '@/utils/toast';
  export default {
    name: 'ChangePassword',
    components: { CCCountDown },
    props: {
      phone: {
        type: String,
        default: ''
      }
    },
    data() {
      return {
        form: {
          telephone: this.phone,
          password: '',
          password2: '',
          authCode: ''
        },
        rules: {
          telephone: [
            { required: true, message: '请输入手机号', trigger: 'blur' }, {
              trigger: 'blur',
              validator: (rule, value, callBack) => {
                if (/^[1][0-9]{10}$/.test(value)) callBack();
                else callBack('手机号码不合法');
              }
            }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 6, message: '密码长度不小于6个字符', trigger: 'blur' }
          ],
          password2: [
            { required: true, message: '请再次输入密码', trigger: 'blur' }
          ],
          nickname: [{ required: true, message: '请输入昵称', trigger: 'blur' }],
          authCode: [
            { required: true, message: '请输入短信验证码', trigger: 'blur' }
          ]
        }
      };
    },

    methods: {
      async onCodeClick() {
        this.rules.telephone[0].validator(null, this.form.telephone, async(msg) => {
          if (msg) return;
          const res = await doSmsCode({ telephone: this.form.telephone });
          if (res.code !== 200) {
            ZZToast.warn(res.message);
          } else {
            this.$refs.codebtn.run();
            this.cache.saveSmsTime();
          }
        });
      },

      async onSure() {
        const valid = await this.$refs.form.validate();
        if (!valid) return;
        const data = { ...this.form };
        data.username = data.telephone;
        delete data.password2;
        const res = await doUpdatePwd(data);
        if (res.code !== 200) {
          ZZToast.warn(res.message);
        } else {
          this.toast.success('密码修改成功，请重新登录');
          await doLogout();
          this.$router.replace('/login');
        }
      }
    },
    mounted() {
      let t = this.cache.fetchSmsTime();
      if (!t) return;
      t = Math.floor((new Date().getTime() - t) / 1000);
      if (t > 0 && t < 60) this.$refs.codebtn.run(60 - t);
    }
  };
</script>
<style lang="scss" type="text/scss" scoped>

.el-form {
  margin-right: 70px;
  color: white;
  .el-input,
  .login-btn {
    width: 100%;
  }
  .login-btn {
    margin-top: 12px;
  }
}

.code-box {
  @include v-center();
  .el-input {
    margin-right: 12px;
  }
}
</style>
